function Event() {
  this._events = {};
}

// 添加事件监听
Event.prototype.addEventListener = function (type, callback) {
  if (typeof callback != 'function') {
    return;
  }

  if (!this._events[type]) {
    this._events[type] = [];
  }

  this._events[type].push(callback);
};

// 移除事件监听
Event.prototype.removeEventListener = function (type, callback) {
  var events = this._events;
  for (var key in events) {
    if (!events.hasOwnProperty(key)) continue;
    if (key === type) {
      var callbacks = events[key];
      // 移除指定的监听函数
      if (callback) {
        for (var i = callbacks.length - 1; i > -1; i--) {
          if (callbacks[i] === callback) {
            callbacks.splice(i, 1);
          }
        }
        // 移除该事件的所有监听函数
      } else {
        events[key] = [];
      }
    }

  }
};

// 触发事件
Event.prototype.fire = function (type, data) {
  var events = this._events;
  var callbacks = events[type] || [];
  callbacks.forEach(function (callback) {
    callback(data);
  });
};

module.exports = Event;
